home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / mpeg / decoders.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  32KB  |  942 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /*
  22.  * decoders.c
  23.  *
  24.  * This file contains all the routines for Huffman decoding required in 
  25.  * MPEG
  26.  *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <assert.h>
  31. #include "decoders.h"
  32. #include "util.h" 
  33. #include "video.h"
  34. #include "proto.h"
  35.  
  36. /* Decoding table for macroblock_address_increment */
  37. mb_addr_inc_entry     mb_addr_inc[2048];
  38.  
  39. /* Decoding table for macroblock_type in predictive-coded pictures */
  40. mb_type_entry         mb_type_P[64];
  41.  
  42. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  43. mb_type_entry         mb_type_B[64];
  44.  
  45. /* Decoding table for motion vectors */
  46. motion_vectors_entry  motion_vectors[2048];
  47.  
  48. /* Decoding table for coded_block_pattern */
  49.  
  50. coded_block_pattern_entry coded_block_pattern[512] = 
  51. { {(unsigned int)ERROR, 0}, {(unsigned int)ERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
  52.     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
  53.     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
  54.     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
  55.     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
  56.     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
  57.     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
  58.     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
  59.     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
  60.     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
  61.     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
  62.     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
  63.     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
  64.     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
  65.     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
  66.     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
  67.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  68.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  69.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  70.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  71.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  72.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  73.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  74.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  75.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  76.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  77.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  78.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  79.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  80.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  81.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  82.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  83.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  84.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  85.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  86.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  87.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  88.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  89.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  90.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  91.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  92.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  93.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  94.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  95.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  96.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  97.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  98.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  99.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  100.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  101.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  102.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
  103.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  104.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  105.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  106.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  107.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  108.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  109.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  110.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  111.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  112.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  113.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  114.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}
  115. };
  116.  
  117. /* Decoding table for dct_dc_size_luminance */
  118. dct_dc_size_entry dct_dc_size_luminance[128] =
  119. {   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  120.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  121.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  122.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  123.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  124.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  125.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  126.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  127.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  128.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  129.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  130.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  131.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  132.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  133.     {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, 
  134.     {6, 5}, {6, 5}, {6, 5}, {6, 5}, {7, 6}, {7, 6}, {8, 7}, {(unsigned int)ERROR, 0}
  135. };
  136.  
  137. /* Decoding table for dct_dc_size_chrominance */
  138. dct_dc_size_entry dct_dc_size_chrominance[256] =
  139. { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  140.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  141.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  142.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  143.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  144.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  145.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  146.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  147.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  148.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  149.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  150.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  151.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  152.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  153.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  154.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  155.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  156.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  157.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  158.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  159.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  160.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  161.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  162.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  163.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  164.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  165.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  166.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  167.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  168.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  169.     {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, 
  170.     {6, 6}, {6, 6}, {6, 6}, {6, 6}, {7, 7}, {7, 7}, {8, 8}, {(unsigned int)ERROR, 0}
  171. };
  172.  
  173. /* DCT coeff tables. */
  174.  
  175. unsigned short int dct_coeff_tbl_0[256] =
  176. {
  177. 0xffff, 0xffff, 0xffff, 0xffff, 
  178. 0xffff, 0xffff, 0xffff, 0xffff, 
  179. 0xffff, 0xffff, 0xffff, 0xffff, 
  180. 0xffff, 0xffff, 0xffff, 0xffff, 
  181. 0x052f, 0x051f, 0x050f, 0x04ff, 
  182. 0x183f, 0x402f, 0x3c2f, 0x382f, 
  183. 0x342f, 0x302f, 0x2c2f, 0x7c1f, 
  184. 0x781f, 0x741f, 0x701f, 0x6c1f, 
  185. 0x028e, 0x028e, 0x027e, 0x027e, 
  186. 0x026e, 0x026e, 0x025e, 0x025e, 
  187. 0x024e, 0x024e, 0x023e, 0x023e, 
  188. 0x022e, 0x022e, 0x021e, 0x021e, 
  189. 0x020e, 0x020e, 0x04ee, 0x04ee, 
  190. 0x04de, 0x04de, 0x04ce, 0x04ce, 
  191. 0x04be, 0x04be, 0x04ae, 0x04ae, 
  192. 0x049e, 0x049e, 0x048e, 0x048e, 
  193. 0x01fd, 0x01fd, 0x01fd, 0x01fd, 
  194. 0x01ed, 0x01ed, 0x01ed, 0x01ed, 
  195. 0x01dd, 0x01dd, 0x01dd, 0x01dd, 
  196. 0x01cd, 0x01cd, 0x01cd, 0x01cd, 
  197. 0x01bd, 0x01bd, 0x01bd, 0x01bd, 
  198. 0x01ad, 0x01ad, 0x01ad, 0x01ad, 
  199. 0x019d, 0x019d, 0x019d, 0x019d, 
  200. 0x018d, 0x018d, 0x018d, 0x018d, 
  201. 0x017d, 0x017d, 0x017d, 0x017d, 
  202. 0x016d, 0x016d, 0x016d, 0x016d, 
  203. 0x015d, 0x015d, 0x015d, 0x015d, 
  204. 0x014d, 0x014d, 0x014d, 0x014d, 
  205. 0x013d, 0x013d, 0x013d, 0x013d, 
  206. 0x012d, 0x012d, 0x012d, 0x012d, 
  207. 0x011d, 0x011d, 0x011d, 0x011d, 
  208. 0x010d, 0x010d, 0x010d, 0x010d, 
  209. 0x282c, 0x282c, 0x282c, 0x282c, 
  210. 0x282c, 0x282c, 0x282c, 0x282c, 
  211. 0x242c, 0x242c, 0x242c, 0x242c, 
  212. 0x242c, 0x242c, 0x242c, 0x242c, 
  213. 0x143c, 0x143c, 0x143c, 0x143c, 
  214. 0x143c, 0x143c, 0x143c, 0x143c, 
  215. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  216. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  217. 0x085c, 0x085c, 0x085c, 0x085c, 
  218. 0x085c, 0x085c, 0x085c, 0x085c, 
  219. 0x047c, 0x047c, 0x047c, 0x047c, 
  220. 0x047c, 0x047c, 0x047c, 0x047c, 
  221. 0x046c, 0x046c, 0x046c, 0x046c, 
  222. 0x046c, 0x046c, 0x046c, 0x046c, 
  223. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  224. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  225. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  226. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  227. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  228. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  229. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  230. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  231. 0x681c, 0x681c, 0x681c, 0x681c, 
  232. 0x681c, 0x681c, 0x681c, 0x681c, 
  233. 0x641c, 0x641c, 0x641c, 0x641c, 
  234. 0x641c, 0x641c, 0x641c, 0x641c, 
  235. 0x601c, 0x601c, 0x601c, 0x601c, 
  236. 0x601c, 0x601c, 0x601c, 0x601c, 
  237. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  238. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  239. 0x581c, 0x581c, 0x581c, 0x581c, 
  240. 0x581c, 0x581c, 0x581c, 0x581c, 
  241. };
  242.  
  243. unsigned short int dct_coeff_tbl_1[16] = 
  244. {
  245. 0x00bb, 0x202b, 0x103b, 0x00ab, 
  246. 0x084b, 0x1c2b, 0x541b, 0x501b, 
  247. 0x009b, 0x4c1b, 0x481b, 0x045b, 
  248. 0x0c3b, 0x008b, 0x182b, 0x441b, 
  249. };
  250.  
  251. unsigned short int dct_coeff_tbl_2[4] =
  252. {
  253. 0x4019, 0x1429, 0x0079, 0x0839, 
  254. };
  255.  
  256. unsigned short int dct_coeff_tbl_3[4] = 
  257. {
  258. 0x0449, 0x3c19, 0x3819, 0x1029, 
  259. };
  260.  
  261. unsigned short int dct_coeff_next[256] = 
  262. {
  263. 0xffff, 0xffff, 0xffff, 0xffff, 
  264. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  265. 0x0826, 0x0826, 0x2416, 0x2416, 
  266. 0x0046, 0x0046, 0x2016, 0x2016, 
  267. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  268. 0x1815, 0x1815, 0x1815, 0x1815, 
  269. 0x0425, 0x0425, 0x0425, 0x0425, 
  270. 0x1415, 0x1415, 0x1415, 0x1415, 
  271. 0x3417, 0x0067, 0x3017, 0x2c17, 
  272. 0x0c27, 0x0437, 0x0057, 0x2817, 
  273. 0x0034, 0x0034, 0x0034, 0x0034, 
  274. 0x0034, 0x0034, 0x0034, 0x0034, 
  275. 0x1014, 0x1014, 0x1014, 0x1014, 
  276. 0x1014, 0x1014, 0x1014, 0x1014, 
  277. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  278. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  279. 0x0023, 0x0023, 0x0023, 0x0023, 
  280. 0x0023, 0x0023, 0x0023, 0x0023, 
  281. 0x0023, 0x0023, 0x0023, 0x0023, 
  282. 0x0023, 0x0023, 0x0023, 0x0023, 
  283. 0x0813, 0x0813, 0x0813, 0x0813, 
  284. 0x0813, 0x0813, 0x0813, 0x0813, 
  285. 0x0813, 0x0813, 0x0813, 0x0813, 
  286. 0x0813, 0x0813, 0x0813, 0x0813, 
  287. 0x0412, 0x0412, 0x0412, 0x0412, 
  288. 0x0412, 0x0412, 0x0412, 0x0412, 
  289. 0x0412, 0x0412, 0x0412, 0x0412, 
  290. 0x0412, 0x0412, 0x0412, 0x0412, 
  291. 0x0412, 0x0412, 0x0412, 0x0412, 
  292. 0x0412, 0x0412, 0x0412, 0x0412, 
  293. 0x0412, 0x0412, 0x0412, 0x0412, 
  294. 0x0412, 0x0412, 0x0412, 0x0412, 
  295. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  296. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  297. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  298. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  299. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  300. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  301. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  302. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  303. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  304. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  305. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  306. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  307. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  308. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  309. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  310. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  311. 0x0011, 0x0011, 0x0011, 0x0011, 
  312. 0x0011, 0x0011, 0x0011, 0x0011, 
  313. 0x0011, 0x0011, 0x0011, 0x0011, 
  314. 0x0011, 0x0011, 0x0011, 0x0011, 
  315. 0x0011, 0x0011, 0x0011, 0x0011, 
  316. 0x0011, 0x0011, 0x0011, 0x0011, 
  317. 0x0011, 0x0011, 0x0011, 0x0011, 
  318. 0x0011, 0x0011, 0x0011, 0x0011, 
  319. 0x0011, 0x0011, 0x0011, 0x0011, 
  320. 0x0011, 0x0011, 0x0011, 0x0011, 
  321. 0x0011, 0x0011, 0x0011, 0x0011, 
  322. 0x0011, 0x0011, 0x0011, 0x0011, 
  323. 0x0011, 0x0011, 0x0011, 0x0011, 
  324. 0x0011, 0x0011, 0x0011, 0x0011, 
  325. 0x0011, 0x0011, 0x0011, 0x0011, 
  326. 0x0011, 0x0011, 0x0011, 0x0011, 
  327. };
  328.  
  329. unsigned short int dct_coeff_first[256] = 
  330. {
  331. 0xffff, 0xffff, 0xffff, 0xffff, 
  332. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  333. 0x0826, 0x0826, 0x2416, 0x2416, 
  334. 0x0046, 0x0046, 0x2016, 0x2016, 
  335. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  336. 0x1815, 0x1815, 0x1815, 0x1815, 
  337. 0x0425, 0x0425, 0x0425, 0x0425, 
  338. 0x1415, 0x1415, 0x1415, 0x1415, 
  339. 0x3417, 0x0067, 0x3017, 0x2c17, 
  340. 0x0c27, 0x0437, 0x0057, 0x2817, 
  341. 0x0034, 0x0034, 0x0034, 0x0034, 
  342. 0x0034, 0x0034, 0x0034, 0x0034, 
  343. 0x1014, 0x1014, 0x1014, 0x1014, 
  344. 0x1014, 0x1014, 0x1014, 0x1014, 
  345. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  346. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  347. 0x0023, 0x0023, 0x0023, 0x0023, 
  348. 0x0023, 0x0023, 0x0023, 0x0023, 
  349. 0x0023, 0x0023, 0x0023, 0x0023, 
  350. 0x0023, 0x0023, 0x0023, 0x0023, 
  351. 0x0813, 0x0813, 0x0813, 0x0813, 
  352. 0x0813, 0x0813, 0x0813, 0x0813, 
  353. 0x0813, 0x0813, 0x0813, 0x0813, 
  354. 0x0813, 0x0813, 0x0813, 0x0813, 
  355. 0x0412, 0x0412, 0x0412, 0x0412, 
  356. 0x0412, 0x0412, 0x0412, 0x0412, 
  357. 0x0412, 0x0412, 0x0412, 0x0412, 
  358. 0x0412, 0x0412, 0x0412, 0x0412, 
  359. 0x0412, 0x0412, 0x0412, 0x0412, 
  360. 0x0412, 0x0412, 0x0412, 0x0412, 
  361. 0x0412, 0x0412, 0x0412, 0x0412, 
  362. 0x0412, 0x0412, 0x0412, 0x0412, 
  363. 0x0010, 0x0010, 0x0010, 0x0010, 
  364. 0x0010, 0x0010, 0x0010, 0x0010, 
  365. 0x0010, 0x0010, 0x0010, 0x0010, 
  366. 0x0010, 0x0010, 0x0010, 0x0010, 
  367. 0x0010, 0x0010, 0x0010, 0x0010, 
  368. 0x0010, 0x0010, 0x0010, 0x0010, 
  369. 0x0010, 0x0010, 0x0010, 0x0010, 
  370. 0x0010, 0x0010, 0x0010, 0x0010, 
  371. 0x0010, 0x0010, 0x0010, 0x0010, 
  372. 0x0010, 0x0010, 0x0010, 0x0010, 
  373. 0x0010, 0x0010, 0x0010, 0x0010, 
  374. 0x0010, 0x0010, 0x0010, 0x0010, 
  375. 0x0010, 0x0010, 0x0010, 0x0010, 
  376. 0x0010, 0x0010, 0x0010, 0x0010, 
  377. 0x0010, 0x0010, 0x0010, 0x0010, 
  378. 0x0010, 0x0010, 0x0010, 0x0010, 
  379. 0x0010, 0x0010, 0x0010, 0x0010, 
  380. 0x0010, 0x0010, 0x0010, 0x0010, 
  381. 0x0010, 0x0010, 0x0010, 0x0010, 
  382. 0x0010, 0x0010, 0x0010, 0x0010, 
  383. 0x0010, 0x0010, 0x0010, 0x0010, 
  384. 0x0010, 0x0010, 0x0010, 0x0010, 
  385. 0x0010, 0x0010, 0x0010, 0x0010, 
  386. 0x0010, 0x0010, 0x0010, 0x0010, 
  387. 0x0010, 0x0010, 0x0010, 0x0010, 
  388. 0x0010, 0x0010, 0x0010, 0x0010, 
  389. 0x0010, 0x0010, 0x0010, 0x0010, 
  390. 0x0010, 0x0010, 0x0010, 0x0010, 
  391. 0x0010, 0x0010, 0x0010, 0x0010, 
  392. 0x0010, 0x0010, 0x0010, 0x0010, 
  393. 0x0010, 0x0010, 0x0010, 0x0010, 
  394. 0x0010, 0x0010, 0x0010, 0x0010, 
  395. };
  396.  
  397. /* Macro for filling up the decoding table for mb_addr_inc */
  398. #define ASSIGN1(start, end, step, val, num) \
  399.   for (i = start; i < end; i+= step) { \
  400.     for (j = 0; j < step; j++) { \
  401.       mb_addr_inc[i+j].value = val; \
  402.       mb_addr_inc[i+j].num_bits = num; \
  403.     } \
  404.     val--; \
  405.     }
  406.  
  407.  
  408.  
  409. /*
  410.  *--------------------------------------------------------------
  411.  *
  412.  * init_mb_addr_inc --
  413.  *
  414.  *    Initialize the VLC decoding table for macro_block_address_increment
  415.  *
  416.  * Results:
  417.  *    The decoding table for macro_block_address_increment will
  418.  *      be filled; illegal values will be filled as ERROR.
  419.  *
  420.  * Side effects:
  421.  *    The global array mb_addr_inc will be filled.
  422.  *
  423.  *--------------------------------------------------------------
  424.  */
  425. static void
  426. init_mb_addr_inc()
  427. {
  428.   int i, j, val;
  429.  
  430.   for (i = 0; i < 8; i++) {
  431.     mb_addr_inc[i].value = ERROR;
  432.     mb_addr_inc[i].num_bits = 0;
  433.   }
  434.  
  435.   mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
  436.   mb_addr_inc[8].num_bits = 11;
  437.  
  438.   for (i = 9; i < 15; i++) {
  439.     mb_addr_inc[i].value = ERROR;
  440.     mb_addr_inc[i].num_bits = 0;
  441.   }
  442.  
  443.   mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
  444.   mb_addr_inc[15].num_bits = 11;
  445.  
  446.   for (i = 16; i < 24; i++) {
  447.     mb_addr_inc[i].value = ERROR;
  448.     mb_addr_inc[i].num_bits = 0;
  449.   }
  450.  
  451.   val = 33;
  452.  
  453.   ASSIGN1(24, 36, 1, val, 11);
  454.   ASSIGN1(36, 48, 2, val, 10);
  455.   ASSIGN1(48, 96, 8, val, 8);
  456.   ASSIGN1(96, 128, 16, val, 7);
  457.   ASSIGN1(128, 256, 64, val, 5);
  458.   ASSIGN1(256, 512, 128, val, 4);
  459.   ASSIGN1(512, 1024, 256, val, 3);
  460.   ASSIGN1(1024, 2048, 1024, val, 1);
  461. }
  462.  
  463.  
  464. /* Macro for filling up the decoding table for mb_type */
  465. #define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
  466.   for (i = start; i < end; i ++) { \
  467.     mb_type[i].mb_quant = quant; \
  468.     mb_type[i].mb_motion_forward = motion_forward; \
  469.     mb_type[i].mb_motion_backward = motion_backward; \
  470.     mb_type[i].mb_pattern = pattern; \
  471.     mb_type[i].mb_intra = intra; \
  472.     mb_type[i].num_bits = num; \
  473.   }
  474.      
  475.  
  476.  
  477. /*
  478.  *--------------------------------------------------------------
  479.  *
  480.  * init_mb_type_P --
  481.  *
  482.  *    Initialize the VLC decoding table for macro_block_type in
  483.  *      predictive-coded pictures.
  484.  *
  485.  * Results:
  486.  *    The decoding table for macro_block_type in predictive-coded
  487.  *      pictures will be filled; illegal values will be filled as ERROR.
  488.  *
  489.  * Side effects:
  490.  *    The global array mb_type_P will be filled.
  491.  *
  492.  *--------------------------------------------------------------
  493.  */
  494. static void
  495. init_mb_type_P()
  496. {
  497.   int i;
  498.  
  499.   mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward 
  500.     = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern 
  501.       = mb_type_P[0].mb_intra = ERROR;
  502.   mb_type_P[0].num_bits = 0;
  503.  
  504.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
  505.   ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
  506.   ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
  507.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
  508.   ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
  509.   ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
  510.   ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
  511. }
  512.  
  513.  
  514.  
  515.  
  516. /*
  517.  *--------------------------------------------------------------
  518.  *
  519.  * init_mb_type_B --
  520.  *
  521.  *    Initialize the VLC decoding table for macro_block_type in
  522.  *      bidirectionally-coded pictures.
  523.  *
  524.  * Results:
  525.  *    The decoding table for macro_block_type in bidirectionally-coded
  526.  *      pictures will be filled; illegal values will be filled as ERROR.
  527.  *
  528.  * Side effects:
  529.  *    The global array mb_type_B will be filled.
  530.  *
  531.  *--------------------------------------------------------------
  532.  */
  533. static void
  534. init_mb_type_B()
  535. {
  536.   int i;
  537.  
  538.   mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward 
  539.     = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern 
  540.       = mb_type_B[0].mb_intra = ERROR;
  541.   mb_type_B[0].num_bits = 0;
  542.  
  543.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
  544.   ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
  545.   ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
  546.   ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
  547.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
  548.   ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
  549.   ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
  550.   ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
  551.   ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
  552.   ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
  553.   ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
  554. }
  555.  
  556.  
  557. /* Macro for filling up the decoding tables for motion_vectors */
  558. #define ASSIGN3(start, end, step, val, num) \
  559.   for (i = start; i < end; i+= step) { \
  560.     for (j = 0; j < step / 2; j++) { \
  561.       motion_vectors[i+j].code = val; \
  562.       motion_vectors[i+j].num_bits = num; \
  563.     } \
  564.     for (j = step / 2; j < step; j++) { \
  565.       motion_vectors[i+j].code = -val; \
  566.       motion_vectors[i+j].num_bits = num; \
  567.     } \
  568.     val--; \
  569.   }
  570.  
  571.  
  572.  
  573. /*
  574.  *--------------------------------------------------------------
  575.  *
  576.  * init_motion_vectors --
  577.  *
  578.  *    Initialize the VLC decoding table for the various motion
  579.  *      vectors, including motion_horizontal_forward_code, 
  580.  *      motion_vertical_forward_code, motion_horizontal_backward_code,
  581.  *      and motion_vertical_backward_code.
  582.  *
  583.  * Results:
  584.  *    The decoding table for the motion vectors will be filled;
  585.  *      illegal values will be filled as ERROR.
  586.  *
  587.  * Side effects:
  588.  *    The global array motion_vector will be filled.
  589.  *
  590.  *--------------------------------------------------------------
  591.  */
  592. static void
  593. init_motion_vectors()
  594. {
  595.   int i, j, val = 16;
  596.  
  597.   for (i = 0; i < 24; i++) {
  598.     motion_vectors[i].code = ERROR;
  599.     motion_vectors[i].num_bits = 0;
  600.   }
  601.  
  602.   ASSIGN3(24, 36, 2, val, 11);
  603.   ASSIGN3(36, 48, 4, val, 10);
  604.   ASSIGN3(48, 96, 16, val, 8);
  605.   ASSIGN3(96, 128, 32, val, 7);
  606.   ASSIGN3(128, 256, 128, val, 5);
  607.   ASSIGN3(256, 512, 256, val, 4);
  608.   ASSIGN3(512, 1024, 512, val, 3);
  609.   ASSIGN3(1024, 2048, 1024, val, 1);
  610. }
  611.  
  612.  
  613.  
  614.  
  615. /*
  616.  *--------------------------------------------------------------
  617.  *
  618.  * init_tables --
  619.  *
  620.  *    Initialize all the tables for VLC decoding; this must be
  621.  *      called when the system is set up before any decoding can
  622.  *      take place.
  623.  *
  624.  * Results:
  625.  *    All the decoding tables will be filled accordingly.
  626.  *
  627.  * Side effects:
  628.  *    The corresponding global array for each decoding table 
  629.  *      will be filled.
  630.  *
  631.  *--------------------------------------------------------------
  632.  */    
  633. void
  634. init_tables()
  635. {
  636.   extern void init_pre_idct();
  637.  
  638.   init_mb_addr_inc();
  639.   init_mb_type_P();
  640.   init_mb_type_B();
  641.   init_motion_vectors();
  642.   init_pre_idct();
  643.  
  644. #ifdef ANALYSIS
  645.   {
  646.     init_stats();
  647.   }
  648. #endif
  649. }
  650.  
  651. /*
  652.  *--------------------------------------------------------------
  653.  *
  654.  * DecodeDCTDCSizeLum --
  655.  *
  656.  *    Huffman Decoder for dct_dc_size_luminance; location where
  657.  *      the result of decoding will be placed is passed as argument.
  658.  *      The decoded values are obtained by doing a table lookup on
  659.  *      dct_dc_size_luminance.
  660.  *
  661.  * Results:
  662.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  663.  *      unbound values will be placed in the location specified.
  664.  *
  665.  * Side effects:
  666.  *    Bit stream is irreversibly parsed.
  667.  *
  668.  *--------------------------------------------------------------
  669.  */        
  670. void
  671. decodeDCTDCSizeLum(value)
  672. unsigned int *value;
  673. {
  674.   unsigned int index;
  675.  
  676.   show_bits7(index);
  677.   
  678.   *value = dct_dc_size_luminance[index].value;
  679.  
  680.   flush_bits(dct_dc_size_luminance[index].num_bits);
  681. }
  682.  
  683.  
  684.  
  685.  
  686. /*
  687.  *--------------------------------------------------------------
  688.  *
  689.  * DecodeDCTDCSizeChrom --
  690.  *
  691.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  692.  *      the result of decoding will be placed is passed as argument.
  693.  *      The decoded values are obtained by doing a table lookup on
  694.  *      dct_dc_size_chrominance.
  695.  *
  696.  * Results:
  697.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  698.  *      unbound values will be placed in the location specified.
  699.  *
  700.  * Side effects:
  701.  *    Bit stream is irreversibly parsed.
  702.  *
  703.  *--------------------------------------------------------------
  704.  */    
  705. void    
  706. decodeDCTDCSizeChrom(value)
  707. unsigned int *value;
  708. {
  709.   unsigned int index;
  710.  
  711.   show_bits8(index);
  712.   
  713.   *value = dct_dc_size_chrominance[index].value;
  714.  
  715.   flush_bits(dct_dc_size_chrominance[index].num_bits);
  716. }
  717.  
  718.  
  719.  
  720. /*
  721.  *--------------------------------------------------------------
  722.  *
  723.  * decodeDCTCoeff --
  724.  *
  725.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  726.  *      locations where the results of decoding: run and level, are to
  727.  *      be placed and also the type of DCT coefficients, either
  728.  *      dct_coeff_first or dct_coeff_next, are being passed as argument.
  729.  *      
  730.  *      The decoder first examines the next 8 bits in the input stream,
  731.  *      and perform according to the following cases:
  732.  *      
  733.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  734.  *                    perform a table lookup on dct_coeff_tbl_0.
  735.  *                    One more bit is then examined to determine the sign
  736.  *                    of level.
  737.  *
  738.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  739.  *                    perform a table lookup on dct_coeff_tbl_1.
  740.  *                    One more bit is then examined to determine the sign
  741.  *                    of level.
  742.  *      
  743.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  744.  *                    perform a table lookup on dct_coeff_tbl_2.
  745.  *                    One more bit is then examined to determine the sign
  746.  *                    of level.
  747.  *
  748.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  749.  *                    perform a table lookup on dct_coeff_tbl_3.
  750.  *                    One more bit is then examined to determine the sign
  751.  *                    of level.
  752.  *
  753.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  754.  *                    value of run is not ESCAPE, extract one more bit
  755.  *                    to determine the sign of level; otherwise 6 more
  756.  *                    bits will be extracted to obtain the actual value 
  757.  *                    of run , and then 8 or 16 bits to get the value of level.
  758.  *                    
  759.  *      
  760.  *
  761.  * Results:
  762.  *    The decoded values of run and level or ERROR for unbound values
  763.  *      are placed in the locations specified.
  764.  *
  765.  * Side effects:
  766.  *    Bit stream is irreversibly parsed.
  767.  *
  768.  *--------------------------------------------------------------
  769.  */
  770. static void
  771. decodeDCTCoeff(dct_coeff_tbl, run, level)
  772. unsigned short int *dct_coeff_tbl;                                       
  773. unsigned int *run;
  774. int *level;
  775. {
  776.   unsigned int temp, index, num_bits;
  777.   unsigned int value, next32bits, flushed;
  778.  
  779.   /*
  780.    * Grab the next 32 bits and use it to improve performance of
  781.    * getting the bits to parse. Thus, calls are translated as:
  782.    *
  783.    *    show_bitsX  <-->   next32bits >> (32-X)
  784.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);
  785.    *               flushed += X;
  786.    *               next32bits &= bitMask[flushed];
  787.    *    flush_bitsX <-->   flushed += X;
  788.    *               next32bits &= bitMask[flushed];
  789.    *
  790.    */
  791.   show_bits32(next32bits);
  792.   flushed = 0;
  793.  
  794.   /* show_bits8(index); */
  795.   index = next32bits >> 24;
  796.  
  797.   if (index > 3) {
  798.     value = dct_coeff_tbl[index];
  799.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  800.     if (*run == END_OF_BLOCK) {
  801.       *level = END_OF_BLOCK;
  802.     }
  803.     else {
  804.       /* num_bits = (value & NUM_MASK) + 1; */
  805.       /* flush_bits(num_bits); */
  806.       flushed = (value & NUM_MASK) + 1;
  807.       next32bits &= bitMask[flushed];
  808.       if (*run != ESCAPE) {
  809.          *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  810.      /* get_bits1(value); */
  811.      /* if (value) *level = -*level; */
  812.      if (next32bits >> (31-flushed)) *level = -*level;
  813.      flushed++;
  814.      /* next32bits &= bitMask[flushed];  last op before update */
  815.        }
  816.        else {    /* *run == ESCAPE */
  817.          /* get_bits14(temp); */
  818.      temp = next32bits >> (18-flushed);
  819.      flushed += 14;
  820.      next32bits &= bitMask[flushed];
  821.      *run = temp >> 8;
  822.      temp &= 0xff;
  823.      if (temp == 0) {
  824.             /* get_bits8(*level); */
  825.         *level = next32bits >> (24-flushed);
  826.         flushed += 8;
  827.         /* next32bits &= bitMask[flushed];  last op before update */
  828.          assert(*level >= 128);
  829.      } else if (temp != 128) {
  830.         /* Grab sign bit */
  831.         *level = ((int) (temp << 24)) >> 24;
  832.      } else {
  833.             /* get_bits8(*level); */
  834.         *level = next32bits >> (24-flushed);
  835.         flushed += 8;
  836.         /* next32bits &= bitMask[flushed];  last op before update */
  837.         *level = *level - 256;
  838.         assert(*level <= -128 && *level >= -255);
  839.      }
  840.        }
  841.        /* Update bitstream... */
  842.        flush_bits(flushed);
  843.     }
  844.   }
  845.   else {
  846.     if (index == 2) { 
  847.       /* show_bits10(index); */
  848.       index = next32bits >> 22;
  849.       value = dct_coeff_tbl_2[index & 3];
  850.     }
  851.     else if (index == 3) { 
  852.       /* show_bits10(index); */
  853.       index = next32bits >> 22;
  854.       value = dct_coeff_tbl_3[index & 3];
  855.     }
  856.     else if (index) {    /* index == 1 */
  857.       /* show_bits12(index); */
  858.       index = next32bits >> 20;
  859.       value = dct_coeff_tbl_1[index & 15];
  860.     }
  861.     else {   /* index == 0 */
  862.       /* show_bits16(index); */
  863.       index = next32bits >> 16;
  864.       value = dct_coeff_tbl_0[index & 255];
  865.     }
  866.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  867.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  868.  
  869.     /*
  870.      * Fold these operations together to make it fast...
  871.      */
  872.     /* num_bits = (value & NUM_MASK) + 1; */
  873.     /* flush_bits(num_bits); */
  874.     /* get_bits1(value); */
  875.     /* if (value) *level = -*level; */
  876.  
  877.     flushed = (value & NUM_MASK) + 2;
  878.     if ((next32bits >> (32-flushed)) & 0x1) *level = -*level;
  879.  
  880.     /* Update bitstream ... */
  881.     flush_bits(flushed);
  882.   }
  883. }
  884.  
  885.  
  886. /*
  887.  *--------------------------------------------------------------
  888.  *
  889.  * decodeDCTCoeffFirst --
  890.  *
  891.  *    Huffman Decoder for dct_coeff_first. Locations for the
  892.  *      decoded results: run and level, are being passed as
  893.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  894.  *      with the table dct_coeff_first.
  895.  *
  896.  * Results:
  897.  *    The decoded values of run and level for dct_coeff_first or
  898.  *      ERROR for unbound values are placed in the locations given.
  899.  *
  900.  * Side effects:
  901.  *    Bit stream is irreversibly parsed.
  902.  *
  903.  *--------------------------------------------------------------
  904.  */        
  905. void
  906. decodeDCTCoeffFirst(run, level)
  907. unsigned int *run;
  908. int *level;
  909. {
  910.   decodeDCTCoeff(dct_coeff_first, run, level);
  911. }
  912.  
  913.  
  914.  
  915.  
  916. /*
  917.  *--------------------------------------------------------------
  918.  *
  919.  * decodeDCTCoeffNext --
  920.  *
  921.  *    Huffman Decoder for dct_coeff_first. Locations for the
  922.  *      decoded results: run and level, are being passed as
  923.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  924.  *      with the table dct_coeff_next.
  925.  *
  926.  * Results:
  927.  *    The decoded values of run and level for dct_coeff_next or
  928.  *      ERROR for unbound values are placed in the locations given.
  929.  *
  930.  * Side effects:
  931.  *    Bit stream is irreversibly parsed.
  932.  *
  933.  *--------------------------------------------------------------
  934.  */ 
  935. void       
  936. decodeDCTCoeffNext(run, level)
  937. unsigned int *run;
  938. int *level;
  939. {
  940.   decodeDCTCoeff(dct_coeff_next, run, level);
  941. }
  942.